package com.tongfusoft.document.web.controller.doc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.doc.service.IAdvancedSearchService;
import com.tongfusoft.document.doc.service.IDocAuthenticationService;
import com.tongfusoft.document.doc.service.IDocService;
import com.tongfusoft.document.entity.CustomSearches;
import com.tongfusoft.document.entity.Docs;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.dictionary.service.IDocumentDictionaryService;
import com.tongfusoft.document.manager.metadata.service.IMetadataService;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;

@Controller
public class DocSearchController extends BaseController {

	@Autowired
	@Qualifier("documentDictionaryService")
	public IDocumentDictionaryService dDictionaryService;

	@Autowired
	@Qualifier("metaDataManagerService")
	public IMetadataService mMService;

	@Autowired
	public IDocService docService;
	@Autowired
	public IAdvancedSearchService advancedSearchService;
	@Autowired
	public IDocAuthenticationService docAuthenticationService;

	@UrlDesc(value = "高级查询页")
	@RequestMapping("/advanced/page")
	public String advancedPage(Model model, String queryText) {

		model.addAttribute("mainPageKey", "advancedQuery");
		model.addAttribute("mainPage", "doc/advancedQuery");
		model.addAttribute("queryTa", queryText);
		model.addAttribute("docTypeList", this.dDictionaryService.getDictByCode("docType"));
		model.addAttribute("groupList", this.docService.docEditGroupByUser(((Users) super.getUser()).getUserId()));
		model.addAttribute("metaDateList", this.mMService.getIsEnableMeta());
		return "base";

	}

	@UrlDesc(value = "所有文档列表页")
	@RequestMapping("/docSearch/list")
	public String docList(Model model, String queryTa, String csId) {

		String searchText = super.getRequest().getParameter("searchText");
		model.addAttribute("mainPageKey", "docSearch");
		model.addAttribute("mainPage", "doc/docSearch");
		model.addAttribute("searchText", StringUtils.isEmpty(searchText) ? "" : searchText);
		model.addAttribute("csId", csId);
		if (StringUtils.isNotEmpty(queryTa)) {
			model.addAttribute("queryText", queryTa);
		} else {
			if (StringUtils.isNotEmpty(csId)) {
				CustomSearches cs = this.advancedSearchService.getDetail(csId, (Users) super.getUser());
				model.addAttribute("queryText", cs == null ? "" : cs.getCsData());
			}

		}

		return "base";

	}

	@UrlDesc(value = "所有文档查询")
	@RequestMapping("/docSearch/query")
	@ResponseBody
	public PageInfo docQuery(Model model, int pageIndex, int pageSize, String query) {
		PageInfo pageInfo = new PageInfo();
		try {
			Docs doc = new Docs();
			if (StringUtils.isNotEmpty(query)) {

				Map queryMap = JsonUtils.getMap4Json(query);
				if (queryMap.get("fastText") != null) {
					model.addAttribute("queryText", query);
					doc.setDocTitle(queryMap.get("fastText").toString());
					doc.setDocSummary(queryMap.get("fastText").toString());
					doc.setOriginalName(queryMap.get("fastText").toString());
				} else {

					String whereSql = this.advancedSearchService
							.generateSQl(this.advancedSearchService.analysis(query).getQueryJson());
					doc.setWhereSql(whereSql);
				}
			}

			pageInfo = this.docService.searchDoc(doc, (Users) super.getUser(), pageSize, pageIndex);

		} catch (Exception e) {

			e.printStackTrace();
		}
		return pageInfo;

	}

	@UrlDesc(value = "所有文档查询")
	@RequestMapping("/docSearch/folder/query")
	@ResponseBody
	public PageInfo folderDocQuery(Model model, int pageIndex, int pageSize, String query) {
		PageInfo pageInfo = new PageInfo();
		try {
			Docs doc = new Docs();
			if (StringUtils.isNotEmpty(query)) {

				Map queryMap = JsonUtils.getMap4Json(query);
				if (queryMap.get("fastText") != null) {
					doc.setDocTitle(queryMap.get("fastText").toString());
					doc.setDocSummary(queryMap.get("fastText").toString());
					doc.setOriginalName(queryMap.get("fastText").toString());
					doc.setDocFolder(queryMap.get("folderId").toString());
				} else {
					String whereSql = this.advancedSearchService
							.generateSQl(this.advancedSearchService.analysis(query).getQueryJson());
					doc.setWhereSql(whereSql);
				}
			}

			pageInfo = this.docService.searchDoc(doc, (Users) super.getUser(), pageSize, pageIndex);

		} catch (Exception e) {

			e.printStackTrace();
		}
		return pageInfo;

	}

	@UrlDesc(value = "保存高级查询条件")
	@RequestMapping("/advanced/save")
	@ResponseBody
	public ResultModel saveAdvanced(Model model, String queryTa, String csId, String oper, String csName) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		CustomSearches cs = new CustomSearches();
		cs.setCsData(queryTa);
		cs.setCsName(csName);
		try {
			if (oper.equals("edit")) {

				cs.setCsId(csId);
				this.advancedSearchService.updateSearchTa(cs, (Users) super.getUser());
				rm.setData(cs.getCsId());
			} else {

				this.advancedSearchService.saveSearchTa(cs, (Users) super.getUser());
				rm.setData(cs.getCsId());
			}
		} catch (Exception e) {

			rm.setState("error");
		}

		return rm;
	}
	/*
	 * @RequestMapping("/docView/advanced/{docName}") public String
	 * docView(@PathVariable("docName") String docName,String docId, Model
	 * model) {
	 * 
	 * 
	 * if (this.docAuthenticationService.isRead(this.docAuthenticationService.
	 * getGroupId(docName, docId), (Users) super.getUser())) {
	 * 
	 * model.addAttribute("docName", docName); model.addAttribute("docId",
	 * docId); model.addAttribute("mainPageKey", "docDetail");
	 * model.addAttribute("mainPage", "doc/docDetail");
	 * model.addAttribute("returnPage", "docSearch/list");
	 * model.addAttribute("metaDateList", this.mMService.getIsEnableMeta());
	 * 
	 * }else{
	 * 
	 * 
	 * model.addAttribute("docName", docName); return
	 * "redirect:/error/docViewDetail"; }
	 * 
	 * return "base";
	 * 
	 * 
	 * }
	 */

}
